// pages/reorderDetail/reorderDetail.js
const app = getApp();
import { reorderdetail, cancelReback } from '../../config/https.js';
import { u_formatTimestamp } from '../../config/tool.js'
import { seckill } from '../../utils/util.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '', // 订单id
    orderInfo: '', // 订单信息
    contact: '', // 电话
    times: '', // 倒计时
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getData();
  },

  // 获取数据
  getData(){
    app.loading();
    let that = this;
    reorderdetail({
      token: app.globalData.userId,
      refund_id: this.data.id
    }).then(res =>{
      wx.hideLoading();
      if(res.code == 1){
        if (res.data.info.countdown){
          that.cutDown(res.data.info.countdown);
        }
        // 申请时间
        if (res.data.info.create_time){
          res.data.info.create_time = u_formatTimestamp(res.data.info.create_time * 1000);
        }
        // 审核时间
        if (res.data.info.review_time){
          res.data.info.review_time = u_formatTimestamp(res.data.info.review_time * 1000);
        }
        // 退款时间
        if (res.data.info.refund_time){
          res.data.info.refund_time = u_formatTimestamp(res.data.info.refund_time * 1000);
        }
        that.setData({
          orderInfo: res.data.info,
          contact: res.data.kf
        })
      }else{
        app.$tip(res.msg);
      }
    })
    wx.hideLoading();
  },

  // 倒计时
  cutDown(time){
    let timmer = setInterval(() =>{
      if(Number(time) > 0){
        time--;
        this.setData({
          times: seckill(time)
        })
      } else {
        clearInterval(timmer)
      }
    },1000)
    this.setData({
      timmer: timmer
    })
  },

  // 取消申请
  cancel(){
    let that = this;
    wx.showModal({
      title: '温馨提示',
      content: '您确定要取消退款申请吗?',
      confirmText: '确定',
      confirmColor: '#57b127',
      success(data){
        if(data.confirm){
          cancelReback({
            token: app.globalData.userId,
            refund_id: that.data.orderInfo.id
          }).then(res => {
            if (res.code == 1) {
              app.$tip(res.msg);
              setTimeout(() => {
                that.setData({
                  times: '',
                })
                that.getData();
              }, 1500)
            } else {
              app.$tip(res.msg);
            }
          })
        }
      }
    })
    
  },

  // 拨打电话
  makeCall(){
    wx.makePhoneCall({
      phoneNumber: this.data.contact,
      success(res){

      },
      fail(err){

      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(this.data.timmer)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.data.timmer)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})